package net.lgpage.service;

import java.io.File;

import com.matriq.cloudmanager.MainActivity.APKUpdateReceiver;
import com.matriq.cloudmanager.dialog.DownloadDialog;
import com.matriq.cloudmanager.dialog.UpdateNoticeDialog;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import net.lgpage.dialog.DialogListener;

public class APKUpdateUtil {  
	  
    private Context mContext;  
    //提示语  
    private String updateMsg;  
    //返回的安装包url  
    private String apkUrl;  
      
    public String getApkUrl() {
		return apkUrl;
	}

	public void setApkUrl(String apkUrl) {
		this.apkUrl = apkUrl;
	}
      
    public APKUpdateUtil(Context context) {  
        this.mContext = context;  
    }  
      
    //外部接口让主Activity调用  
    public void checkUpdateInfo(){  
    	registerReceiver();
    	startUpdateValidate();
    }  
    
    public void startUpdateValidate() {
		// 绑定Service
		Intent intent = new Intent(mContext, APKUpdateService.class);
		mContext.startService(intent);
	}
    
    APKUpdateReceiver receiver;
	public void registerReceiver() {
		// 动态注册广播接收器
		receiver = new APKUpdateReceiver();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("com.lg.broadcast.apkupdate");
		mContext.registerReceiver(receiver, intentFilter);
	}
	
	/**
	 * 广播接收器
	 * @author len
	 */
	public class APKUpdateReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String url = intent.getStringExtra("url");
			String must = intent.getStringExtra("must");
			String content = intent.getStringExtra("content");
			showNoticeDialog(content,url);
		}
	}
	
	UpdateNoticeDialog noticeDialog;

	private void showNoticeDialog(String content,final String apkUrl) {
		if (noticeDialog == null) {
			noticeDialog = new UpdateNoticeDialog(mContext);
			noticeDialog.setData(content);
			noticeDialog.setListener(new DialogListener() {

				@Override
				public void onDialogExecute(Object... obj) {
					showDownloadDialog(apkUrl);
				}
			});
		}
		noticeDialog.show();
	}

	private void showDownloadDialog(String apkUrl) {
		DownloadDialog dd = new DownloadDialog(mContext);
		dd.setData("更新包下载中", apkUrl, "dowish.apk");
		dd.setListener(new DialogListener() {
			
			@Override
			public void onDialogExecute(Object... os) {
				File file = (File) os[0];
				installApk(file);
			}
		});
		dd.show();
		dd.download();
	}
     /**
     * 安装apk
     * @param url
     */  
    private void installApk(File apkfile){  
        if (!apkfile.exists()) {  
            return;  
        }      
        Intent i = new Intent(Intent.ACTION_VIEW);  
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");   
        mContext.startActivity(i);  
    }  
    
}  